<?php

	class showTable{
		//主机名
		protected $host = 'localhost';
		
		//数据库账号
		protected $username = 'root';
		
		//数据库密码
		protected $password = 'root';
		
		//数据库
		protected $dbName = 'test';
		
		protected $conn;

		protected $where;

		protected $page;

		//分页数
		protected $limit = 3;
		
		public function __construct($where = null, $page = 1){
			$this->conn = mysqli_connect($this->host, $this->username, $this->password);
			if(!$this->conn){
				die('Could not connect: ' . mysql_error());
			}
			
			$this->selectDb();

			if($where)  $this->where = $this->doWhere($where);
            $this->page = $page;
		}
		
		public function selectDb(){
			mysqli_select_db($this->conn, $this->dbName);
		}

		public function total(){
            $sql = "select count(id) as total from records $this->where";
            $result = mysqli_query($this->conn, $sql);
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

            if(!$row['total'])  $row['total'] = 0;
            return $row['total'];
        }
		
		public function selectSql(){
		    if($this->page == 1){
                $start = 0;
            }else{
		        $start = $this->limit * ($this->page - 1);
            }
			$sql = "select * from records $this->where order by time desc limit $start, $this->limit ";
			$result = mysqli_query($this->conn, $sql);

			if (!$result) {
				printf("Error: %s\n", mysqli_error($this->conn));
				exit();
			}

			$i = 0;
			$row_array = array();
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                $row_array[$i] = $row;
				$i++;
			}
			
			return $row_array;
		}

        public function doWhere($where){
            if(!$where)  return false;

            $str = ' where 1=1 ';
            foreach($where as $key=>$val){
                if($val)  $str .= ' and ' . $key . '=' . $val;
            }

            return $str;
        }
		
		public function showTb(){
			$result = $this->selectSql();

			return $result;
		}
		
	}


class page {

    private $myde_total;          //总记录数
    private $myde_size;           //一页显示的记录数
    private $myde_page;           //当前页
    private $myde_page_count;     //总页数
    private $myde_i;              //起头页数
    private $myde_en;             //结尾页数
    private $myde_url;            //获取当前的url
    /*
     * $show_pages
     * 页面显示的格式，显示链接的页数为2*$show_pages+1。
     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
     */
    private $show_pages;

    public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {
        $this->myde_total = $this->numeric($myde_total);
        $this->myde_size = $this->numeric($myde_size);
        $this->myde_page = $this->numeric($myde_page);
        $this->myde_page_count = ceil($this->myde_total / $this->myde_size);
        $this->myde_url = $myde_url;
        if ($this->myde_total < 0)
            $this->myde_total = 0;
        if ($this->myde_page < 1)
            $this->myde_page = 1;
        if ($this->myde_page_count < 1)
            $this->myde_page_count = 1;
        if ($this->myde_page > $this->myde_page_count)
            $this->myde_page = $this->myde_page_count;
        $this->limit = ($this->myde_page - 1) * $this->myde_size;
        $this->myde_i = $this->myde_page - $show_pages;
        $this->myde_en = $this->myde_page + $show_pages;
        if ($this->myde_i < 1) {
            $this->myde_en = $this->myde_en + (1 - $this->myde_i);
            $this->myde_i = 1;
        }
        if ($this->myde_en > $this->myde_page_count) {
            $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
            $this->myde_en = $this->myde_page_count;
        }
        if ($this->myde_i < 1)
            $this->myde_i = 1;
    }

    //检测是否为数字
    private function numeric($num) {
        if (strlen($num)) {
            if (!preg_match("/^[0-9]+$/", $num)) {
                $num = 1;
            } else {
                $num = substr($num, 0, 11);
            }
        } else {
            $num = 1;
        }
        return $num;
    }

    //地址替换
    private function page_replace($page) {
        return str_replace("{page}", $page, $this->myde_url);
    }

    //首页
    private function myde_home() {
        if ($this->myde_page != 1) {
            return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";
        } else {
            return "<p>首页</p>";
        }
    }

    //上一页
    private function myde_prev() {
        if ($this->myde_page != 1) {
            return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title='上一页'>上一页</a>";
        } else {
            return "<p>上一页</p>";
        }
    }

    //下一页
    private function myde_next() {
        if ($this->myde_page != $this->myde_page_count) {
            return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title='下一页'>下一页</a>";
        } else {
            return"<p>下一页</p>";
        }
    }

    //尾页
    private function myde_last() {
        if ($this->myde_page != $this->myde_page_count) {
            return "<a href='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</a>";
        } else {
            return "<p>尾页</p>";
        }
    }

    //输出
    public function myde_write($id = 'page') {
        $str = "<div id=" . $id . ">";
        $str.=$this->myde_home();
        $str.=$this->myde_prev();
        if ($this->myde_i > 1) {
            $str.="<p class='pageEllipsis'>...</p>";
        }
        for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
            if ($i == $this->myde_page) {
                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";
            } else {
                $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";
            }
        }
        if ($this->myde_en < $this->myde_page_count) {
            $str.="<p class='pageEllipsis'>...</p>";
        }
        $str.=$this->myde_next();
        $str.=$this->myde_last();
        $str.="<p class='pageRemark'>共<b>" . $this->myde_page_count .
            "</b>页<b>" . $this->myde_total . "</b>条数据</p>";
        $str.="</div>";
        return $str;
    }

}

	
	$uid = @$_GET['uid'] ? $_GET['uid'] : '';
	$type = @$_GET['type'] ? $_GET['type'] : '';
	$page = @$_GET['page'] ? $_GET['page'] : 1;
	
	$db = new showTable(array('uid'=>$uid, 'type'=>$type), $page);
	$result = $db->showTb();
	$total = $db->total();

	$pageUrl = new page($total, 3, $page, "./showData.php?uid=$uid&type=$type&page={page}");
    
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
	<style>
		table
		{
			border-collapse: collapse;
			margin: 50px auto;
			text-align: center;
			width: 1200px;
		}
		table td, table th
		{
			border: 1px solid #cad9ea;
			color: #666;
			height: 30px;
		}
		table thead th
		{
			background-color: #CCE8EB;
			width: 100px;
		}
		table tr:nth-child(odd)
		{
			background: #fff;
		}
		table tr:nth-child(even)
		{
			background: #F5FAFA;
		}


.pager {
    text-align: right;
}

.pager a {
    padding: 3px 8px;
    margin-left: 3px;
    line-height: 20px;
    background: #f9f9f9;
    border: 1px solid #DBDBDB;
    text-decoration: none
}

.pager a:hover,
.pager a.current {
    background-color: #7CD5B1;
    color: #fff;
    border: 1px solid #7CD5B1;
    cursor: pointer;
}

.page {
    text-align: center;
    margin: 50px 0
}

.page a, .page span.prev_disabled {
    border: 1px solid #ededed;
    color: #3d3d3d;
    font-weight: 700;
    height: 35px;
    line-height: 35px;
    margin-left: 5px;
    min-width: 9px;
    padding: 0 13px;
    text-align: center;
    text-decoration: none;
    vertical-align: top;
    font-family: "simsun";
    display: inline-block
}

.page span.prev_disabled {
    cursor: default;
    color: #ccc;
    margin: 0 10px 0 0
}

.page a.current {
    background-color: #f40;
    border-color: #f40;
    color: #fff;
    font-weight: 700;
    position: relative;
    z-index: 1;
}

.page .extra {
    display: inline-block;
    margin-left: 10px;
    height: 35px;
    line-height: 35px;
    color: #656565;
}

.page .page-num {
    border: 1px solid #ededed;
    height: 21px;
    text-align: center;
    width: 35px;
    display: inline-block
}

.page .page-submit {
    background-clip: padding-box;
    border: 1px solid #ededed;
    border-radius: 2px;
    cursor: pointer;
    height: 21px;
    line-height: 21px;
    text-align: center;
    width: 43px;
    display: inline-block
}

.page .page-submit:hover {
    border-color: #f40;
    color: #f40;
}

.page a:focus, .page a:hover {
    border-color: #f40;
    z-index: 1;
}

.loading {
    margin: 30px 0;
    text-align: center
}

p {
    margin: 0
}

#page {
    height: 40px;
    padding: 20px 0px;
	float: right;
}

#page a {
    display: block;
    float: left;
    margin-right: 10px;
    padding: 2px 12px;
    height: 24px;
    border: 1px #cccccc solid;
    background: #fff;
    text-decoration: none;
    color: #808080;
    font-size: 12px;
    line-height: 24px;
}

#page a:hover {
    color: #077ee3;
    border: 1px #077ee3 solid;
}

#page a.cur {
    border: none;
    background: #077ee3;
    color: #fff;
}

#page p {
    float: left;
    padding: 2px 12px;
    font-size: 12px;
    height: 24px;
    line-height: 24px;
    color: #bbb;
    border: 1px #ccc solid;
    background: #fcfcfc;
    margin-right: 8px;

}

#page p.pageRemark {
    border-style: none;
    background: none;
    margin-right: 0px;
    padding: 4px 0px;
    color: #666;
}

#page p.pageRemark b {
    color: red;
}

#page p.pageEllipsis {
    border-style: none;
    background: none;
    padding: 4px 0px;
    color: #808080;
}

.dates li {
    font-size: 14px;
    margin: 20px 0
}

.dates li span {
    text-align: center
}

td {
    font-size: 15px;
    margin: 20px 0;
}
	</style>
</head>
<body>
	
    <table>
		<tr>
			<th colspan="7">
				<form action="" method="get" >
				    用户ID：<input type="text" name="uid" value="<?=@$_GET['uid'] ? $_GET['uid'] : ''?>" >
				    用户type：<input type="text" name="type" value="<?=@$_GET['type'] ? $_GET['type'] : ''?>" >
				    <input type="submit">
				</form>
			</th>
		</tr>
		<tr>
			<th>ID</th>
			<th>uid</th>
			<th>type</th>
			<th>changeCoins</th>
			<th>newCoins</th>
            <th>time</th>
			<th>strJson</th>
		</tr>
        <?php
            foreach($result as $val){
        ?>
        <tr>
            <td><?=trim($val['ID'], 0)?></td>
            <td><?=$val['uid']?></td>
            <td><?=trim($val['type'], 0)?></td>
            <td><?=$val['changeCoins']?></td>
            <td><?=$val['newCoins']?></td>
            <td><?=$val['time']?></td>
            <td <?php if($val['strJson']){ ?>onclick="return clickShow('<?=$val['ID']?>');" style="cursor: pointer;" <?php } ?> ><?php if($val['strJson']){echo "[详情]";}else{echo "[无]";} ?></td>
        </tr>
		<tr class="show_<?=$val['ID']?>" style="display: none;" >
			<td colspan="7"><?=$val['strJson']?></td>
		</tr>
        <?php } ?>
		<tr>
			<td colspan="7">
				<?=$pageUrl->myde_write()?>
			</td>
		</tr>
	</table>
	<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
	<script>
		
		var clickShow = function(id){
			if(!id)  return false;
			
			$(".show_"+id).toggle();
		}
	</script>
</body>
</html>

